package cn.xiaosiyuan.util.sql;

import cn.xiaosiyuan.util.X;
import cn.xiaosiyuan.util.interfaces.ISqlUtil;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static cn.xiaosiyuan.util.XMLFile.XmlUtil.*;

public class MysqlUtil implements ISqlUtil {

    public String path = X.pathUtil.getByRelativePath("/xml2sql/config/mysql_field_type_config.xml");

    @Override
    public String getCreateTableSql(HashMap<String, List<Map<String, String>>> map) {

        StringBuilder rs = new StringBuilder();

        for (Map.Entry<String, List<Map<String, String>>> entry : map.entrySet()) {
            String key = entry.getKey();
            List<Map<String, String>> field = entry.getValue();


            // 先遍历一遍，找出所有主键字段
            Map<String, String> primMap = new HashMap<>();
            for (Map<String, String> fieldMap : field) {
                String tagName = fieldMap.getOrDefault(TAG_NAME, "");
                String attrFieldPriField = fieldMap.getOrDefault(ATTR_FIELD_FIELD, "");
                switch(tagName){
                    case TAG_TYPE_PRIM_KEY: {
                        primMap.put(attrFieldPriField, "");
                        break;
                    }
                    default:
                        break;
                }
            }


            Generator generator = new Generator();

            generator.setTbCreateBegin(key);
            for (Map<String, String> fieldMap : field) {
                String tagName = fieldMap.getOrDefault(TAG_NAME, "");

                String attrEntityTitle = fieldMap.getOrDefault(ATTR_TABLE_TITLE, "");

                String attrFieldName = fieldMap.getOrDefault(ATTR_FIELD_NAME, "");
                String attrFieldType = fieldMap.getOrDefault(ATTR_FIELD_TYPE, "");
                String attrFieldDesc = fieldMap.getOrDefault(ATTR_FIELD_DESC, "");
                String attrFieldPriField = fieldMap.getOrDefault(ATTR_FIELD_FIELD, "");


                switch(tagName){
                    case TAG_TYPE_ENTITY: {
                        generator.setTbCreateEnd(attrEntityTitle);
                        break;
                    }
                    case TAG_TYPE_FIELD: {
                        if (primMap.containsKey(attrFieldName)) {
                            // 主键字段是 NOT NULL
                            generator.setField(attrFieldName, X.xmlUtil.getValue(path, attrFieldType), "NOT NULL", attrFieldDesc);
                        } else {
                            // 非主键字段是 DEFAULT NULL
                            generator.setField(attrFieldName, X.xmlUtil.getValue(path, attrFieldType), "DEFAULT NULL", attrFieldDesc);
                        }
                        break;
                    }
                    case TAG_TYPE_PRIM_KEY: {
                        generator.setPrimKey(attrFieldPriField);
                        break;
                    }
                    default:
                        break;
                }

            }
            rs.append(generator.getSql());
        }

        return rs.toString();
    }

    class Generator {

        public StringBuilder sql = new StringBuilder();


        public String getSql() {
            sql.append(tbCreateBegin.toString());
            sql.append(field.toString());
            sql.append(primKey.toString());
            sql.append(tbCreateEnd.toString());
            return sql.toString();
        }


        public String tbCreateBeginTemplate = " CREATE TABLE `%s` (\n";
        //  `id` varchar(255) COLLATE utf8_bin NOT NULL COMMENT '主键',
        public String fieldTemplate = "\t`%s` %s COLLATE utf8_bin %s COMMENT '%s',\n";
        // PRIMARY KEY (`id`)
        public String primKeyTemplate = "\tPRIMARY KEY (`id`)\n";
        public String tbCreateEndTemplate = " ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin COMMENT='%s';\n";

        public StringBuilder tbCreateBegin = new StringBuilder();
        public StringBuilder field = new StringBuilder();
        public StringBuilder primKey = new StringBuilder();
        public StringBuilder tbCreateEnd = new StringBuilder();

        public void setTbCreateBegin(String tableName) {
            this.tbCreateBegin.append(String.format(this.tbCreateBeginTemplate, tableName));
        }

        public void setField(String name, String type, String defaultVal, String comment) {
            this.field.append(String.format(this.fieldTemplate, name, type, defaultVal, comment));
        }

        public void setPrimKey(String prim_key) {
            this.primKey.append(String.format(this.primKeyTemplate, prim_key));
        }

        public void setTbCreateEnd(String tbDesc) {
            this.tbCreateEnd.append(String.format(this.tbCreateEndTemplate, tbDesc));
        }

    }



}
